﻿using System.Collections.Generic;
using System.Linq;

namespace SnadBoxExample1.Models
{
    public class DataManager
    {
        private readonly EntityModelExmaple _entityModel;

        public DataManager()
        {
            _entityModel = new EntityModelExmaple();
        }

        /// <summary>
        /// for example return storeg type view list
        /// </summary>
        /// <returns></returns>
        public List<ViewStore> GetViewStores()
        {
            return (from itm in _entityModel.StoreDevices
                    join sysType in _entityModel.SystemTypes on itm.SystemTypeID equals sysType.ID
                    join typDev in _entityModel.TypeDevices on itm.TypeID equals typDev.ID
                    join modelDev in _entityModel.ModelDevices on itm.ModelID equals modelDev.ID
                    join typeRep in _entityModel.TypeRepairs on itm.TypeRepair.ID equals typeRep.ID
                    select new ViewStore
                    {
                        DeviceId = sysType.ID,
                        DeviceTypeId = typDev.ID,
                        DeviceTypeName = typDev.Title,
                        ModeDeviceId = modelDev.ID,
                        ModeDeviceTitle = modelDev.Title,
                        StoreDeviceComment = itm.Comment,
                        StoreDeviceSerilDate1 = itm.DataProduction,
                        StoreDeviceSerilDate2 = itm.DataChecking,
                        StoreDeviceSerilNumber = itm.SerialNumber,
                        SystemId = sysType.ID,
                        SystemName = sysType.Title,
                        TypeRepairId = typeRep.ID,
                        TypeRepairTitle = typeRep.Title
                    }).ToList();
        }

        /// <summary>
        /// for example return all rows list(table)
        /// </summary>
        /// <returns></returns>
        public List<SystemType> GetSystemTypes()
        {
            return _entityModel.SystemTypes.ToList();
        }

        public StoreDevice FindById(int id)
        {
            return _entityModel.StoreDevices.FirstOrDefault(n => n.ID == id);
        }
    }
}